import {
  CodeUploadFailData,
  EngineerUploadFailData,
  GoodBOMImportFailData,
  NetworkUploadFailData,
  OrderUploadFailData,
  CommodityUploadFailData,
  NetworkIssuedAMountImportFailData,
} from '@/views/excel/types'
import useExcel from './useExcel'
import moment from 'moment'

export default () => {
  const { timestampToYYYYMMDDHHMMSS } = useExcel()

  /**
   * 获取条码导入失败信息excel导出数据
   * @param {Array<CodeUploadFailData>} sources - 条码导入失败信息
   * @returns {Array<Array<String>>}
   */
  const getSnCodeFailDataExcelExport = (sources: Array<CodeUploadFailData>) => {
    const result = [
      [
        '序号',
        'excel行号',
        '导入失败原因',
        '条码',
        '创建时间',
        '条码类型',
        '物料编号',
        '物料型号',
        '物料规格',
        '生产日期',
        '条码激活状态',
        '激活时间',
        '条码质保状态',
        '登记日期',
        '质保周期',
        '公司姓名',
        '登记公司地址',
      ],
    ]

    sources.forEach((item, index: number) => {
      const { province, city, area, address } = item
      result.push([
        (index + 1).toString(),
        item.excelRowNum ? `${item.excelRowNum}` : '',
        item.excelMsg,
        item.code,
        item.createTime ? timestampToYYYYMMDDHHMMSS(item.createTime) : '',
        item.type ? item.type : '',
        item.goodsCode,
        item.goodsModel,
        item.goodsSpec,
        item.produceDate ? timestampToYYYYMMDDHHMMSS(item.produceDate) : '',
        item.active || '',
        item.activeTime ? timestampToYYYYMMDDHHMMSS(item.activeTime) : '',
        item.status || '',
        item.registerTime ? timestampToYYYYMMDDHHMMSS(item.registerTime) : '',
        item.warrantyDay ? item.warrantyDay.toString() : '',
        item.username,
        province && city && area && address
          ? `${item.province}-${item.city}-${item.area}-${item.address}`
          : '',
      ])
    })

    return result
  }

  const getNetworkFailDataExcelExport = (sources: Array<NetworkUploadFailData>) => {
    const result = [
      [
        '序号',
        '网点名称',
        '网点编号',
        '团队模式',
        '网点模式',
        '负责人名称',
        '负责人手机号',
        '一级网点分类',
        '二级网点分类',
        '三级网点分类',
        '网点机构名称',
        '网点地址',
      ],
    ]

    sources.forEach((item, index) => {
      const { province, city, area, address } = item
      result.push([
        (index + 1).toString(),
        item.name,
        item.code,
        item.teamType,
        item.category,
        item.username,
        item.phone,
        item.networkType,
        item.twoNetworkType,
        item.threeNetworkType,
        item.networkAgency,
        province && city && area && address
          ? `${item.province}-${item.city}-${item.area}-${item.address}`
          : '',
      ])
    })

    return result
  }

  const getOrderFailDataExcelExport = (sources: Array<OrderUploadFailData>) => {
    const result = [
      [
        '序号',
        'excel行号',
        '错误消息',
        '工单单号',
        '工单类型',
        '工单状态',
        '工单质保状态',
        '质保周期',
        '报单时间',
        '工单等级',
        '完成时间',
        '公司姓名',
        '联系人名称',
        '联系人手机号',
        '公司报单地址',
        '条码',
        '物料编号',
        '物料型号',
        '物料购买时间',
        '网点名称',
        '网点接单时间',
        '工程师名称',
        '工程师手机号',
        '客服名称',
        '创建人',
        '工单来源',
        '工单描述',
      ],
    ]

    sources.forEach((item, index) => {
      result.push([
        (index + 1).toString(),
        item.excelRowNum ? item.excelRowNum.toString() : '',
        item.excelMsg,
        item.workNo,
        item.module,
        item.status,
        item.overinsured,
        item.warrantyDay ? `${item.warrantyDay}` : '',
        item.createTime ? moment(item.createTime).format('YYYY-MM-DD') : '',
        item.level,
        item.endTime ? moment(item.endTime).format('YYYY-MM-DD') : '',
        item.username,
        item.userContact,
        item.userPhone,
        `${item.province} ${item.area} ${item.city} ${item.address}`,
        item.code,
        item.goodsCode,
        item.goodsModel,
        item.goodsBuyTime ? moment(item.goodsBuyTime).format('YYYY-MM-DD') : '',
        item.network,
        item.networkReceivingOrdersTime
          ? moment(item.networkReceivingOrdersTime).format('YYYY-MM-DD')
          : '',
        item.engineer,
        item.engineerPhone,
        item.serviceEmp,
        item.createEmp,
        item.source,
        item.content,
      ])
    })

    return result
  }
  const gethistoryFailDataExcelExport = (sources) => {
    const result = [
      [
        // '序号',
        // 'excel行号',
        '导入失败原因',
        '工单单号',
        '创建人',
        '报单时间',
        '工单状态',
        '工单来源',
        '工单类型',
        '用户类型',
        '公司姓名',
        '公司手机号',
        '用户座机',
        '用户其他电话',
        '省',
        '市',
        '县',
        '详细地址',
        '详细地址',
        '服务经理',
        '催单次数',
        '物料购买时间',
        '购买商场',
        '保修类型',
        '品牌',
        '京东工单号',
        '产品大类',
        '产品小类',
        '物料编号',
        '物料型号',
        '物料规格',
        '数量',
        '公司故障描述',
        '故障现象条码',
        '故障现象',
        '故障原因条码',
        '故障原因',
        '维修措施',
        '调度状态',
        '调度人',
        '用户评价',
        '物料状态',
        '外部单号',
        '网点机构',
        '网点编号',
        '网点名称',
        '网点手机',
        '网点最终结算',
        '网点服务费',
        '网点调整金额',
        '网点调整原因',
        '网点申请金额',
        '网点申请原因',
        '远程公里数',
        '远程费',
        '物料申请金额',
        '物料核准金额',
        '单价',
        '发布时间',
        '分派机构时间',
        '承接时间',
        '是否确认接单',
        '确认接单时间',
        '预约人',
        '首次预约时间',
        '首次预约操作时间',
        '预约时间',
        '预约操作时间',
        '完工时间',
        '第一次完工时间',
        '完工类型',
        '完工备注',
        '完工确认人',
        '完工确认时间',
        '取消人',
        '取消时间',
        '取消备注',
        '服务工程师',
        '外部系统传单标识',
        '外部系统传单返回信息',
        '工单备注',
        '旺旺号',
        '是否核销',
        '录单条码',
        '完工条码',
        '亩核不通过原因',
        '最新跟进时间',
        '最新跟进过程',
        '回访满意度',
        '中心审核时间',
        '简易回访状态',
        '简易总体满意情况',
        '简易回访情况',
        '简易回访失败原因',
        '简易回访时间',
        '简易回访备注',
      ],
    ]

    sources.forEach((item, index: number) => {
      result.push([
        // (index + 1).toString(),
        // item.excelRowNo ? item.excelRowNo.toString() : '',
        item.excelErrMsg,
        item.workNo,
        item.createEmp,
        item.orderCreateTime,
        item.status,
        item.source,
        item.orderType,
        item.userType,
        item.username,
        item.userPhone,
        item.userTelehone,
        item.userOtherPhone,
        item.province,
        item.city,
        item.area,
        item.userAddress,
        item.address,
        item.serviceManager,
        item.reminderQty,
        item.goodsBuyTime,
        item.buyMarket,
        item.warrantyType,
        item.brand,
        item.jbOrderNo,
        item.productBigType,
        item.productSmallType,
        item.goodsCode,
        item.goodsModel,
        item.goodsSpec,
        item.qtyStr,
        item.faultDesc,
        item.faultPhenomenonCode,
        item.faultPhenomenon,
        item.faultReasonCode,
        item.faultReason,
        item.maintenanceMeasures,
        item.dispatchStatus,
        item.dispatchEmp,
        item.userEvaluate,
        item.spareStatus,
        item.outOrderNo,
        item.networkMechanism,
        item.networkCode,
        item.network,
        item.networkPhone,
        item.networkEndSettlement,
        item.networkServiceFee,
        item.networkAdjustFee,
        item.networkAdjustReason,
        item.networkApplyFee,
        item.networkApplyReason,
        item.remoteMileage,
        item.remoteFee,
        item.applySpareFee,
        item.spareApprovalFee,
        item.price,
        item.releaseTime,
        item.dispatchMechanismTime,
        item.undertakingTime,
        item.sureReceivingOrder,
        item.sureReceivingOrdersTime,
        item.reservationEmp,
        item.firstReservationTime,
        item.firstReservationOperateTime,
        item.reservationTime,
        item.reservationOperateTime,
        item.endOrderTime,
        item.firstEndOrderTime,
        item.endOrderType,
        item.endOrderRemark,
        item.endOrderSureEmp,
        item.endOrderSureTime,
        item.cancelEmp,
        item.cancelTime,
        item.cancelRemark,
        item.engineer,
        item.outSystemOrderNo,
        item.outSystemOrderReturnInfo,
        item.orderRemark,
        item.wangwangNumber,
        item.writeOff,
        item.recordBarcode,
        item.endOrderBarcode,
        item.checkNotPassReason,
        item.lastFollowUpTime,
        item.lastFollowUpProcess,
        item.followUpSatisfactionLevel,
        item.centerCheckTime,
        item.simpleFollowUpStatus,
        item.simpleOverallSatisfactionLevel,
        item.simpleFollowUpSituation,
        item.simpleFollowUpFailReason,
        item.simpleFollowUpTime,
        item.simpleFollowUpRemark,
      ])
    })

    return result
  }
  // 导入历史工单错误列表
  const DataJson = () => {
    const arr = [
      { workNo: '工单单号' },
      { createEmp: '创建人' },
      { orderCreateTime: '报单时间' },
      { status: '工单状态' },
      { source: '工单来源' },
      { orderType: '工单类型' },
      { userType: '用户类型' },
      { username: '公司姓名' },
      { userPhone: '公司手机号' },
      { userTelehone: '用户座机' },
      { userOtherPhone: '用户其他电话' },
      { province: '省' },
      { city: '市' },
      { area: '县' },
      { userAddress: '详细地址' },
      { address: '详细地址' },
      { serviceManager: '服务经理' },
      { reminderQty: '催单次数' },
      { goodsBuyTime: '物料购买时间' },
      { buyMarket: '购买商场' },
      { warrantyType: '保修类型' },
      { brand: '品牌' },
      { jbOrderNo: '京东工单号' },
      { productBigType: '产品大类' },
      { productSmallType: '产品小类' },
      { goodsCode: '物料编号' },
      { goodsModel: '物料型号' },
      { goodsSpec: '物料规格' },
      { qtyStr: '数量' },
      { faultDesc: '公司故障描述' },
      { faultPhenomenonCode: '故障现象条码' },
      { faultPhenomenon: '故障现象' },
      { faultReasonCode: '故障原因条码' },
      { faultReason: '故障原因' },
      { maintenanceMeasures: '维修措施' },
      { dispatchStatus: '调度状态' },
      { dispatchEmp: '调度人' },
      { userEvaluate: '用户评价' },
      { spareStatus: '物料状态' },
      { outOrderNo: '外部单号' },
      { networkMechanism: '网点机构' },
      { networkCode: '网点编号' },
      { network: '网点名称' },
      { networkPhone: '网点手机' },
      { networkEndSettlement: '网点最终结算' },
      { networkServiceFee: '网点服务费' },
      { networkAdjustFee: '网点调整金额' },
      { networkAdjustReason: '网点调整原因' },
      { networkApplyFee: '网点申请金额' },
      { networkApplyReason: '网点申请原因' },
      { remoteMileage: '远程公里数' },
      { remoteFee: '远程费' },
      { applySpareFee: '物料申请金额' },
      { spareApprovalFee: '物料核准金额' },
      { price: '单价' },
      { releaseTime: '发布时间' },
      { dispatchMechanismTime: '分派机构时间' },
      { undertakingTime: '承接时间' },
      { sureReceivingOrder: '是否确认接单' },
      { sureReceivingOrdersTime: '确认接单时间' },
      { reservationEmp: '预约人' },
      { firstReservationTime: '首次预约时间' },
      { firstReservationOperateTime: '首次预约操作时间' },
      { reservationTime: '预约时间' },
      { reservationOperateTime: '预约操作时间' },
      { endOrderTime: '完工时间' },
      { firstEndOrderTime: '第一次完工时间' },
      { endOrderType: '完工类型' },
      { endOrderRemark: '完工备注' },
      { endOrderSureEmp: '完工确认人' },
      { endOrderSureTime: '完工确认时间' },
      { cancelEmp: '取消人' },
      { cancelTime: '取消时间' },
      { cancelRemark: '取消备注' },
      { engineer: '服务工程师' },
      { outSystemOrderNo: '外部系统传单标识' },
      { outSystemOrderReturnInfo: '外部系统传单返回信息' },
      { orderRemark: '工单备注' },
      { wangwangNumber: '旺旺号' },
      { writeOff: '是否核销' },
      { recordBarcode: '录单条码' },
      { endOrderBarcode: '完工条码' },
      { checkNotPassReason: '审核不通过原因' },
      { lastFollowUpTime: '最新跟进时间' },
      { lastFollowUpProcess: '最新跟进过程' },
      { followUpSatisfactionLevel: '回访满意度' },
      { centerCheckTime: '中心审核时间' },
      { simpleFollowUpStatus: '简易回访状态' },
      { simpleOverallSatisfactionLevel: '简易总体满意情况' },
      { simpleFollowUpSituation: '简易回访情况' },
      { simpleFollowUpFailReason: '简易回访失败原因' },
      { simpleFollowUpTime: '简易回访时间' },
      { simpleFollowUpRemark: '简易回访备注' },
    ]
    return arr.map((item) => {
      // 假设我们只关心第一个非id的键和值
      let str = ''
      Object.keys(item).filter((key) => {
        str = key
      })

      return {
        keyName: str,
        value: item[str],
      } // 或者返回一个空对象或null，根据你的需求
    })
  }

  const getEngineerFailDataExcelExport = (sources: Array<EngineerUploadFailData>) => {
    const result = [
      [
        '序号',
        'excel行号',
        '导入失败原因',
        '工程师姓名',
        '工程师手机号',
        '角色管理',
        '工程师等级',
        '工程师分类',
        '所属网点',
        '工程师技能',
      ],
    ]

    sources.forEach((item, index) => {
      result.push([
        (index + 1).toString(),
        String(item.excelRowNum),
        item.excelMsg || '',
        item.username || '',
        item.phone || '',
        item.superManager ? '管理员' : '工程师',
        item.networkEmpLevel || '',
        item.networkEmpType || '',
        item.network || '',
        item.networkEmpSkill || '',
      ])
    })

    return result
  }

  /**
   * 获取物料Bom导入失败后需要导出的数据
   * @param {Array<GoodBOMImportFailData>} sources - 原导入失败数据
   */
  const getGoodBomImportFailDataForExcelExport = (sources: Array<GoodBOMImportFailData>) => {
    const result = [
      [
        '序号',
        'excel行号',
        '导入失败原因',
        '物料编号',
        '物料类型',
        '物料名称',
        '物料规格',
        '物料备注（组合件）',
        '物料子件条码',
        '物料子件类型',
        '物料子件名称',
        '物料子件规格',
        '物料子件数量',
        '物料子件备注',
      ],
    ]

    sources.forEach((item, index) => {
      result.push([
        (index + 1).toString(),
        String(item.excelRowNum),
        item.excelMsg || '',
        item.goodsCode || '',
        item.goodsTypeStr || '',
        item.goodsName || '',
        item.goodsSpec || '',
        item.goodsRemark || '',
        item.boomGoodsCode || '',
        item.boomGoodsType === 1 ? '成品' : '物料',
        item.boomGoodsName || '',
        item.boomGoodsSpec || '',
        item.boomGoodsQty ? item.boomGoodsQty.toString() : '',
        item.boomGoodsRemark || '',
      ])
    })

    return result
  }

  /**
   * 获取物料导入失败后需要导出的数据
   * @param {Array<>} sources - 原导入失败数据
   */
  const getGoodImportFailDataForExcelExport = (sources: Array<CommodityUploadFailData>) => {
    const result = [
      [
        '序号',
        'excel行号',
        '导入失败原因',
        '物料名称',
        '物料类型',
        '物料品牌',
        '物料型号',
        '物料编号',
        '物料货号',
        '物料分类',
        '物料规格',
        '物料单位',
        '单价',
        '质保周期',
        '是否为核心件',
      ],
    ]

    sources.forEach((item, index) => {
      result.push([
        (index + 1).toString(),
        String(item.excelRowNum),
        item.excelMsg || '',
        item.goodsName || '',
        item.type || '',
        item.goodsBrand || '',
        item.goodsModel || '',
        item.goodsCode || '',
        item.goodsItemNumber || '',
        item.goodsType || '',
        item.goodsSpecName || '',
        item.goodsUnit || '',
        item.price || '',
        item.warrantDay || '',
        item.coreMaterials || '',
      ])
    })

    return result
  }

  /**
   * 获取网点已发放金额导入失败后需要导出的数据
   * @param {Array<>} sources - 原导入失败数据
   */
  const getNetworkIssuedAmountImportFailDataForExcelExport = (
    sources: Array<NetworkIssuedAMountImportFailData>,
  ) => {
    const result = [['序号', 'excel行号', '导入失败原因', '网点名称', '免费配件额度']]

    sources.forEach((item, index) => {
      result.push([
        (index + 1).toString(),
        String(item.excelRowNum),
        item.errorMsg || '',
        item.networkName || '',
        String(item.grantAmount)
      ])
    })

    return result
  }

  return {
    getOrderFailDataExcelExport,
    getSnCodeFailDataExcelExport,
    getNetworkFailDataExcelExport,
    gethistoryFailDataExcelExport,
    getEngineerFailDataExcelExport,
    DataJson,
    getGoodBomImportFailDataForExcelExport,
    getGoodImportFailDataForExcelExport,
    getNetworkIssuedAmountImportFailDataForExcelExport,
  }
}
